<?php
namespace app\index\controller;
use think\Controller;
use Common\Controller\BaseController;
use think\Request;
use think\Session;
use think\Cookie;
class loan extends Controller
{
    public function loan(){
        $user = session('member');
        if(!$user){
            header("Location:../../index/login/login");
        }
        $id['memberid'] = session('member.id');
        $res = db('bankcard')->where($id)->select();
        $this->assign('list',$res);
        return $this->fetch();
        //return $this->fetch();
    }
    public function loanadd(){
        $user = session('member');
        if(!$user){
            header("Refresh:1;url=../../index/login/login");
        }
        if(!$_POST){
            echo "<script>alert('提交为空');javascript:history.go(-1);</script>";
        }
        $data['quota'] = trim(input('quota'));
        $data['bankname'] = trim(input('bankname'));
        $data['bankaccount'] = trim(input('bankaccount'));
        $data['emergencycontact1'] = trim(input('emergencycontact1'));
        $data['relationship1'] = trim(input('relationship1'));
        $data['emergencycontact2'] = trim(input('emergencycontact2'));
        $data['relationship2'] = trim(input('relationship2'));
        $data['emergencycontact3'] = trim(input('emergencycontact3'));
        $data['relationship3'] = trim(input('relationship3'));
        $data['repayment'] = trim(input('repayment'));
        $data['purpose'] = trim(input('purpose'));
        $data['memberid'] = session('member.id');
        $data['phone'] = session('member.phone');
        $data['time'] = time();
        $data['examine'] = 0;
        $data['state'] = 0 ;
        $data['strtotime'] = strtotime("+1 month");
        $res = db('loan')->insert($data);
        if($res){
            echo "<script>alert('请耐心，等待审核');javascript:history.go(-1);</script>";
        }else{
            echo "<script>alert('借款失败，等待审核');javascript:history.go(-1);</script>";
        }
    }
    public function adopt(){
        $user = session('member');
        if(!$user){
            header("Location:../../index/login/login");
        }
        $id['memberid'] = session('member.id');
        //echo strtotime("+1 month");exit;
        $res = db('bystages')->where($id)->select();
        //dump($res);exit;
        $this->assign('list',$res);
        return $this->fetch();
    }
    public function adoptt(){
        $user = session('member');
        if(!$user){
            header("Location:../../index/login/login");
        }
        $id['memberid'] = session('member.id');
        //echo strtotime("+1 month");exit;
        $res = db('short')->where($id)->select();
        //dump($res);exit;
        $this->assign('list',$res);
        return $this->fetch();
    }
    public function still(){
        $user = session('member');
        if(!$user){
            header("Location:../../index/login/login");
        }
        $id['memberid'] = session('member.id');
        //echo strtotime("+1 month");exit;

        $res = db('bystages')->where($id)->select();
        //dump($res['']['']);exit;
        //dump($res);exit;
        $this->assign('list',$res);
        return $this->fetch();
    }
    //分期开始
    public function bystages(){
        $user = session('member');
        if(!$user){
            header("Location:../../index/login/login");
        }
        $id['memberid'] = session('member.id');
        $res = db('bankcard')->where($id)->select();
        $this->assign('list',$res);
        return $this->fetch();
    }
    public function bystagesadd(){
    $user = session('member');
    if(!$user){
        header("Location:../../index/login/login");
    }
//        $a  = 100;
//        $b = 2;
//        echo  $a*1/$b;exit;
    //dump($_POST);exit;

    $data['memberid'] = session('member.id');
    $data['pay']    =$pay= trim(input('pay'));
    $data['bystages']   =$bystages= trim(input('bystages'));
    $data['repayment'] = trim(input('repayment'));
    $data['purpose'] = trim(input('purpose'));
    $payee['id'] = trim(input('payee'));
    $data['examine'] = trim(input('bystages'));
    $data['time'] = time();
    $data['bystagespay'] = $pay*1/$bystages;
    $re = db('bankcard')->where($payee)->find();
    $data['payee'] = $re['cardnumber'];
    $data['phone'] = session('member.phone');
    $a = trim(input('bystages'));
    $data['stime'] = strtotime("+".$a." month");
//        $data[''] = trim(input(''));
//        $data[''] = trim(input(''));
//        $data[''] = trim(input(''));
//        $data[''] = trim(input(''));
//        $data[''] = trim(input(''));
    // dump($bystages['bystages']);exit;

    //dump($data['bystagespay']);exit;
    $res = db('bystages')->insertGetId($data);
    cookie('loan', $res);
//        $b = cookie('loan');
//            dump($b);exit;
    if($res){
        cookie('loan', $res);
        //header("Location:../../index/loan/agreement");
         echo "<script>window.location.href='../../../../index/loan/agreement';</script>";
    }else{
        echo "<script>alert('申请已提交，请耐心等待审核');window.location.href='../../../../index/index/index';</script>";
    }
}
    //分期结束
    public function loan_add(){
        $user = session('member');
        if(!$user){
            header("Location:../../index/login/login");
        }
//        $a  = 100;
//        $b = 2;
//        echo  $a*1/$b;exit;
        //dump($_POST);exit;

        $data['memberid'] = session('member.id');
        $data['pay']    =$pay= trim(input('pay'));
        //$data['bystages']   =$bystages= trim(input('bystages'));
        $data['repayment'] = trim(input('repayment'));
        $data['purpose'] = trim(input('purpose'));
        $payee['id'] = trim(input('payee'));
        //$data['examine'] = trim(input('bystages'));
        $data['time'] = time();
//        $data['bystagespay'] = $pay*1/$bystages;
        $data['stime'] = strtotime("+ 7 day");
//        $aaa = strtotime("+ 1 day");
//        dump($aaa);exit;
        $re = db('bankcard')->where($payee)->find();
        $data['payee'] = $re['cardnumber'];
        $data['phone'] = session('member.phone');
//        $data[''] = trim(input(''));
//        $data[''] = trim(input(''));
//        $data[''] = trim(input(''));
//        $data[''] = trim(input(''));
//        $data[''] = trim(input(''));
        // dump($bystages['bystages']);exit;

        //dump($data['bystagespay']);exit;
        $res = db('short')->insertGetId($data);
        if($res){
            cookie('short', $res);
            echo "<script>window.location.href='../../../../index/loan/agreementt';</script>";
        }else{
            echo "<script>alert('申请已提交，请耐心等待审核');window.location.href='../../../../index/index/index';</script>";
        }
    }
    public function agreement(){
        $user = session('member');
        if(!$user){
            header("Location:../../index/login/login");
        }
        $data['memberid'] = session('member.id');
//        $a = 1;
//        $stime =  strtotime("+".$a." month");
//        dump($stime);exit;
$id['id'] = cookie('loan');
        $res = db('bystages')->where($id)->find();
        $re = db('user')->where($data)->find();
        $this->assign('list',$res);
        $this->assign('listt',$re);
        return $this->fetch();
    }
    public function agreementupdata(){
        $user = session('member');
        if(!$user){
            header("Location:../../index/login/login");
        }
        $data['id'] = trim(input('id'));
//dump($data);exit;
        $agreement['agreement'] = 1;
        $res = db('bystages')->where($data)->update($agreement);
        if($res){
            echo "<script>alert('您已成功同意协议,请等待电话联系');window.location.href='../../../../index/index/index';</script>";
        }else{
            echo "<script>alert('失败请重新操作');javascript:history.go(-1);</script>";
        }
    }
    public function agreementt(){
        $user = session('member');
        if(!$user){
            header("Location:../../index/login/login");
        }
        $data['memberid'] = session('member.id');
//        $a = 1;
//        $stime =  strtotime("+".$a." month");
//        dump($stime);exit;
        $id['id'] = cookie('short');
        $res = db('short')->where($id)->find();
        $re = db('user')->where($data)->find();
        $this->assign('list',$res);
        $this->assign('listt',$re);
        return $this->fetch();
    }
    public function agreementupdataa(){
        $user = session('member');
        if(!$user){
            header("Location:../../index/login/login");
        }
        $data['id'] = trim(input('id'));
//dump($data);exit;
        $agreement['agreement'] = 1;
        $res = db('short')->where($data)->update($agreement);
        if($res){
            echo "<script>alert('您已成功同意协议,请等待电话联系');window.location.href='../../../../index/index/index';</script>";
        }else{
            echo "<script>alert('失败请重新操作');javascript:history.go(-1);</script>";
        }
    }
}